package com.baike.servlet;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.baike.dao.FileDao;
import com.baike.model.FileObj;

@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = -6944622822737627059L;

	private Connection conn;
	ResultSet rs = null;
	FileDao fdao = new FileDao();

	public UploadServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 閺傚洣娆㈡稉濠佺炊
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletContext servletContext = this.getServletConfig().getServletContext();
		File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
		factory.setRepository(repository);
		ServletFileUpload upload = new ServletFileUpload(factory);

		// 閺傚洣娆㈢仦鐐达拷褎鏆熼幑顔肩氨閹垮秳缍旂�电钖�
		FileDao filedao = new FileDao();

		// 鐏忓攤ile鐎电钖勬潪顒�瀵叉稉绨�son鐎涙顑佹稉锟�
		FileObj fileObj = new FileObj();
		//Gson gson = new Gson();
		// String fileGsonObj = gson.toJson(fileObj);

		HttpSession session = request.getSession();
		int author_id = (Integer) session.getAttribute("author_id");
		int entry_id = (Integer) session.getAttribute("entry_id");

		try {
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iterator = items.iterator();
			while (iterator.hasNext()) {
				FileItem item = (FileItem) iterator.next();

				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString();
				} else {
					// 娑撳﹣绱堕弬鍥︽閹垮秳缍�
					String fieldName = item.getFieldName();
					String fileName = item.getName();
					fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
					String contentType = item.getContentType();
					boolean isInMemory = item.isInMemory();
					long sizeInBytes = item.getSize();
					File uploadedFile = new File("D:/Temp/" + fileName);
					item.write(uploadedFile);

					// 閸愭瑥鍙嗛弫鐗堝祦鎼存挻鎼锋担锟�
					Date now = new Date();// 閼惧嘲褰囩化鑽ょ埠閺冨爼妫�
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 閺嶇厧绱￠崠鏍ㄦ闂傦拷

					fileObj.setFile_name(fileName);
					fileObj.setEntry_id(entry_id);
					fileObj.setUpload_time(dateFormat.format(now));
					fileObj.setPath(uploadedFile.getAbsolutePath());
					fileObj.setAuthor_id(author_id);

					filedao.upload(fileObj);
				}
			}

		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.getWriter().println("success");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
